*** do-file for ejpr paper (forthcoming)
** title: dynamics of protest and electoral politics in the great recession
** authors: björn bremer (bremer@mpifg.de), swen hutter and hanspeter kriesi
** description: analyse the pm-level data for the placebo test (appendix c-1)
** last updated: 13 October 2019

clear
set more off 
cd "" // set your local working directory which contains the folders with a sub-folder for the data, graphs, tables, and appendix here

**---------------------------------------
*** set the scheme for the graphs
**---------------------------------------

*ssc install blindschemes, replace
set scheme  plottigblind
set scheme plotplainblind

**---------------------------------------
*** load the pm-level dataset
**---------------------------------------

use "./data/data_placebo_pm_replication.dta", clear

**---------------------------------------
*** create new and recode existing variables
**---------------------------------------

** create the misery factor
factor gdp_change unemployment_change debt_change 
predict misery

** create a simple election number count
recode election_number (-1 0 = 0) (1 = 1) (2 3 4 5 = 2), gen(election_simple)

** recode electoral loss into positive values
gen elect_loss = pm_vote_change*(-1)

** weight protest for duration of the legislative period
gen wevent_dur = wevent/duration
gen wpart_dur = lpopw_part_all/duration

gen react_wevent_dur = react_wevent/duration
gen react_wpart_dur = react_lpopw_part_all/duration

** standardise variables
egen z2_wevent = std(wevent_dur)
egen z2_wpart = std(wpart_dur)
egen z2_react_wevent = std(react_wevent_dur)
egen z2_react_wpart = std(react_lpopw_part_all)
egen z2_elect_loss = std(elect_loss)
egen z2_misery = std(misery)
egen z2_unemployment_change = std(unemployment_change)
egen z2_debt_change = std(debt_change)
egen z2_gdp_change = std(gdp_change)

** relabel variables
lab var z2_elect_loss "Electoral loss"
lab var z2_wevent "Protest"
lab var z2_wpart "Protest"
lab var z2_misery "Misery"
lab var z2_unemployment_change "Unempl."
lab var z2_debt_change "Debt"
lab var z2_gdp_change "GDP"

lab var pm_t_vote "Vote share (t-1)"
lab var misery "Misery"
lab var elect_loss "Electoral loss"
lab var wevent "Protest"

lab var election_simple "Election"
lab def election_simple 0 "Pre-crisis elec." 1 "First crisis elec." 2 "Later crisis elec."
lab val election_simple election_simple

lab var east "Eastern Europe"
lab def east 0 "Not Eastern Europe" 1 "Eastern Europe" 
lab val east east

**---------------------------------------
*** table c-1: 
**---------------------------------------

cd "./appendix/tables/"

eststo clear

eststo: quietly regress z2_elect_loss z2_misery 
eststo: quietly regress z2_wevent z2_misery

eststo: quietly regress z2_elect_loss c.z2_misery##i.election_simple
eststo: quietly regress z2_wevent c.z2_misery##i.election_simple 

eststo: quietly regress z2_elect_loss z2_misery if east == 0 
eststo: quietly regress z2_wevent z2_misery if east == 0 

eststo: quietly regress z2_elect_loss c.z2_misery##i.election_simple if east == 0 
eststo: quietly regress z2_wevent c.z2_misery##i.election_simple if east == 0 

esttab using table_c1.rtf, b(2) t(2) r2(2) nogaps nobaselevels label replace ///
starlevels(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
title(The impact of economic misery on electoral loss and protest)

**---------------------------------------
*** table c-2: test interactions between misery and non-economic protests
**---------------------------------------

eststo clear

eststo: quietly regress z2_elect_loss z2_misery
eststo: quietly regress z2_elect_loss z2_misery z2_wevent
eststo: quietly regress z2_elect_loss c.z2_misery##c.z2_wevent

eststo: quietly regress z2_elect_loss z2_misery if east == 0
eststo: quietly regress z2_elect_loss z2_misery z2_wevent if east == 0
eststo: quietly regress z2_elect_loss c.z2_misery##c.z2_wevent if east == 0

esttab using table_c2.rtf, b(2) t(2) r2(2) nogaps nobaselevels label replace ///
starlevels(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
title(The impact of economic misery and protest on electoral loss)

**---------------------------------------
*** use the parties dataset
**---------------------------------------

clear 
cd "/Users/BjoernBremer/Dropbox/Joined/protest_paper/Two worlds_Electoral punishment and protest politics/Stata/Syntax/replication" //set your local working directory here

**---------------------------------------
*** load the party-level dataset
**---------------------------------------

use "./data/data_placebo_party_replication.dta", clear

**---------------------------------------
*** create new and recode existing variables
**---------------------------------------

** create a misery factor
factor gdp_change unemployment_change debt_change
predict misery

** create a simple election number count
recode election_number (-1 0 = 0) (1 = 1) (2 3 4 5 = 2), gen(election_simple)

** recode electoral loss into positive values
gen elect_loss = vote_change*(-1)

** weight protest for duration of the legislative period
gen wevent_dur = wevent/duration
gen wpart_dur = lpopw_part_all/duration

** standardise variables
egen z2_wevent = std(wevent_dur)
egen z2_wpart = std(wpart_dur)
egen z2_elect_loss = std(elect_loss)
egen z2_vote_change = std(vote_change)
egen z2_misery = std(misery)
egen z2_unemployment_change = std(unemployment_change)
egen z2_debt_change = std(debt_change)
egen z2_gdp_change = std(gdp_change)

** relabel some variables
lab var z2_vote_change "Vote change"
lab var z2_elect_loss "Electoral loss"
lab var misery "Misery"

lab def election_simple 0 "Pre-crisis Elec." 1 "First crisis elec." 2 "Later crisis elec."
lab val election_simple election_simple 

lab var z2_wevent "Protest"
lab var z2_wpart "Protest"
lab var z2_misery "Misery"
lab var z2_unemployment_change "Unempl."
lab var z2_debt_change "Debt"
lab var z2_gdp_change "GDP"

lab var east "Eastern Europe"
lab def east 0 "Not Eastern Europe" 1 "Eastern Europe" 
lab val east east

**---------------------------------------
*** table 5:
**---------------------------------------

cd "./appendix/tables/"

eststo clear
eststo: quietly regress z2_elect_loss i.prime_minister i.government c.z2_wevent##i.party_mainstream c.z2_misery if east == 0
eststo: quietly regress z2_elect_loss i.prime_minister i.government c.z2_wevent##i.party_mainstream##c.z2_misery if east == 0
eststo: quietly regress z2_elect_loss i.prime_minister i.government c.z2_wevent##i.party_left c.z2_misery if east == 0
eststo: quietly regress z2_elect_loss i.prime_minister i.government c.z2_wevent##i.party_left##c.z2_misery if east == 0

esttab using table_c3.rtf, b(2) t(2) r2(2) nogaps nobaselevels label replace ///
starlevels(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
title(The effect of misery and protest on the electoral loss of different parties in Western Europe)
